﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Xml.XPath;

namespace SharingCenter.Web.Classes
{
    public static class SiteConfig
    {
        static readonly string SiteConfigPath = string.Format("{0}/SiteConfig.xml", WebConsts.APPDATA_DIR);
        //static XDocument doc;
        static Dictionary<SiteConfigSection, Dictionary<string, string>> sectionDictionary;

        #region ISiteConfigBase Members

        public static void WriteSetting(SiteConfigSection section, string key, object val)
        {
            //HttpContext.Current.Cache.Remove("SITE_CONFIG");
            string v = val.ToString();
            GetSiteConfigDocument();
            
            if (!sectionDictionary.ContainsKey(section))
            {
                Dictionary<string, string> config = new Dictionary<string, string>();
                config.Add(key, v);
                sectionDictionary.Add(section, config);
            }
            else
            {
                if (!sectionDictionary[section].ContainsKey(key))
                {
                    sectionDictionary[section].Add(key, v);
                }
                else
                {
                    sectionDictionary[section][key] = v;
                }
            }    
        }

        public static void SubmitChanges()
        {
            GetSiteConfigDocument();
            XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(SiteConfigPath));
            
            foreach(KeyValuePair<SiteConfigSection, Dictionary<string, string>> sec in sectionDictionary)
            {
                XElement elSection = doc.Root.XPathSelectElement("Section[@Name='" + sec.Key + "']");
                if (elSection == null)
                {
                    elSection = new XElement("Section",
                        new XAttribute("Name", sec.Key));
                    doc.Root.Add(elSection);
                }

                foreach (KeyValuePair<string, string> conf in sec.Value)
                {
                    XElement elConfig = elSection.XPathSelectElement("Config[@Name='" + conf.Key + "']");
                    if (elConfig == null)
                    {
                        elConfig = new XElement("Config",
                            new XAttribute("Name", conf.Key));
                        elConfig.Value = conf.Value;
                        elSection.Add(elConfig);
                    }
                    else
                    {
                        elConfig.Value = conf.Value;
                    }
                }
            }

            doc.Save(HttpContext.Current.Server.MapPath(SiteConfigPath));
            HttpContext.Current.Application["SITE_CONFIG"] = sectionDictionary;
        }

        public static List<Dictionary<string, string>> GetSettingSection(SiteConfigSection section)
        {
            GetSiteConfigDocument();
            List<Dictionary<string, string>> ret = new List<Dictionary<string, string>>();
            try
            {
                ret.Add(sectionDictionary[section]);
            }
            catch { ret.Add(new Dictionary<string, string>()); }
            return ret;
        }

        public static string ReadSetting(SiteConfigSection section, string key)
        {
            GetSiteConfigDocument();

            if (!sectionDictionary.ContainsKey(section))
            {
                Dictionary<string, string> config = new Dictionary<string, string>();
                config.Add(key, "");
                sectionDictionary.Add(section, config);
            }
            else
            {
                if (!sectionDictionary[section].ContainsKey(key))
                {
                    sectionDictionary[section].Add(key, "");
                }
            }    

            return sectionDictionary[section][key];
        }

        static void GetSiteConfigDocument()
        {
            try
            {
                sectionDictionary = (Dictionary<SiteConfigSection, Dictionary<string, string>>)HttpContext.Current.Application["SITE_CONFIG"];
            }
            catch
            {
                sectionDictionary = null;
            }

            if (sectionDictionary == null || sectionDictionary.Count == 0)
            {
                if (HttpContext.Current.Cache["SITE_CONFIG"] == null)
                {
                    HttpContext.Current.Cache["SITE_CONFIG"] = XDocument.Load(HttpContext.Current.Server.MapPath(SiteConfigPath));
                }
                XDocument doc = (XDocument)HttpContext.Current.Cache["SITE_CONFIG"];
                Dictionary<string, string> configDictionary;
                foreach (XElement elSection in doc.Root.Elements())
                {
                    configDictionary = new Dictionary<string, string>();
                    foreach (XElement elConfig in elSection.Elements())
                    {
                        configDictionary.Add(elConfig.Attribute("Name").Value, elConfig.Value);
                    }

                    SiteConfigSection s = (SiteConfigSection)Enum.Parse(typeof(SiteConfigSection), elSection.Attribute("Name").Value);

                    try
                    {
                        sectionDictionary.Add(s, configDictionary);
                    }
                    catch { }
                }

                HttpContext.Current.Application["SITE_CONFIG"] = sectionDictionary;
            }

            
        }
        #endregion
    }
}
